package com.htc.android.footprints.overlay;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.WeakHashMap;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;
import com.htc.android.footprints.commonui.TBarDrawable;
import com.htc.android.footprints.commonui.TBarGroupDrawable;
import com.htc.android.footprints.utils.FootprintUtils;
import com.htc.android.footprints.vo.Footprint;

import dalvik.system.VMRuntime;

public abstract class AbstractGroupableOverlay extends Overlay
{
  private static final int MINIMUM_TOUCH_DIAMETER = 0;
  private static final int[] PRESSED_STATE_SET;
  private static final String TAG = "AbstractGroupableOverlay";
  private int MSG_ZOOM_BEGIN = 1;
  private int MSG_ZOOM_END = 0;
  private Map<String, Drawable> cachedMap = Collections.synchronizedMap(new WeakHashMap());
  private boolean isZoomChanging = true;
  private Context mContext;
  private GroupDispatcher mGroupDispatcher = new GroupDispatcher(null);
  private Handler mHandler = new Handler()
  {
    public void handleMessage(Message paramMessage)
    {
      if (paramMessage.what == AbstractGroupableOverlay.this.MSG_ZOOM_BEGIN)
        AbstractGroupableOverlay.access$202(AbstractGroupableOverlay.this, true);
      while (true)
      {
        return;
        if (paramMessage.what == AbstractGroupableOverlay.this.MSG_ZOOM_END)
        {
          AbstractGroupableOverlay.access$202(AbstractGroupableOverlay.this, false);
          continue;
        }
      }
    }
  };
  private ArrayList<Footprint> mItemList = new ArrayList(30);
  private Projection mProjection;
  private final Point mTempPoint = new Point();
  private final Point mTempPoint1 = new Point();
  private final Rect mTouchableBounds = new Rect();
  protected Handler pHandler;
  private int zoomLevel = -1;

  static
  {
    int[] arrayOfInt = new int[1];
    arrayOfInt[0] = 16842919;
    PRESSED_STATE_SET = arrayOfInt;
  }

  public AbstractGroupableOverlay()
  {
    HandlerThread localHandlerThread = new HandlerThread("AbstractGroupableOverlay");
    localHandlerThread.start();
    this.pHandler = new Handler(localHandlerThread.getLooper());
  }

  private ArrayList<GroupMapItem> checkItemAtLocation(int paramInt1, int paramInt2, MapView paramMapView)
  {
    ArrayList localArrayList = new ArrayList();
    Iterator localIterator = this.mGroupDispatcher.getGroupMapItems().iterator();
    while (localIterator.hasNext())
    {
      GroupMapItem localGroupMapItem = (GroupMapItem)localIterator.next();
      paramMapView.getProjection().toPixels(localGroupMapItem.getPoint(), this.mTempPoint);
      if (!hitTest(localGroupMapItem, paramInt1 - this.mTempPoint.x, paramInt2 - this.mTempPoint.y))
        continue;
      localArrayList.add(localGroupMapItem);
    }
    return localArrayList;
  }

  private ArrayList<Footprint> getItemAtLocation(int paramInt1, int paramInt2, MapView paramMapView)
  {
    ArrayList localArrayList = new ArrayList();
    Iterator localIterator = this.mGroupDispatcher.getGroupMapItems().iterator();
    while (localIterator.hasNext())
    {
      GroupMapItem localGroupMapItem = (GroupMapItem)localIterator.next();
      paramMapView.getProjection().toPixels(localGroupMapItem.getPoint(), this.mTempPoint);
      if (!hitTest(localGroupMapItem, paramInt1 - this.mTempPoint.x, paramInt2 - this.mTempPoint.y))
        continue;
      localArrayList.addAll(localGroupMapItem.getFootprints());
    }
    return localArrayList;
  }

  private Rect getTouchableBounds(Rect paramRect)
  {
    int i = paramRect.width();
    int j = paramRect.height();
    if ((i >= MINIMUM_TOUCH_DIAMETER) && (j >= MINIMUM_TOUCH_DIAMETER));
    for (Rect localRect = paramRect; ; localRect = this.mTouchableBounds)
    {
      return localRect;
      int k = paramRect.centerX();
      int m = paramRect.centerY();
      int n = Math.max(MINIMUM_TOUCH_DIAMETER, i);
      int i1 = k - n / 2;
      int i2 = Math.max(MINIMUM_TOUCH_DIAMETER, j);
      int i3 = m - i2 / 2;
      this.mTouchableBounds.set(i1, i3, i1 + n, i3 + i2);
    }
  }

  public void changeZoom()
  {
    this.mHandler.removeMessages(this.MSG_ZOOM_END);
    this.isZoomChanging = true;
    this.mHandler.sendEmptyMessageDelayed(this.MSG_ZOOM_END, 700L);
  }

  protected abstract Footprint createItem(int paramInt);

  public void draw(Canvas paramCanvas, MapView paramMapView, boolean paramBoolean)
  {
    super.draw(paramCanvas, paramMapView, paramBoolean);
    if (this.mContext == null)
      this.mContext = paramMapView.getContext();
    this.mProjection = paramMapView.getProjection();
    if (this.zoomLevel != paramMapView.getZoomLevel())
    {
      if (VMRuntime.getRuntime().getExternalBytesAllocated() > 8000000.0D)
        System.gc();
      changeZoom();
      this.zoomLevel = paramMapView.getZoomLevel();
    }
    if (this.isZoomChanging)
    {
      this.mGroupDispatcher.mGroupList.size();
      this.mGroupDispatcher.clear();
      Iterator localIterator2 = this.mItemList.iterator();
      while (localIterator2.hasNext())
      {
        Footprint localFootprint2 = (Footprint)localIterator2.next();
        this.mGroupDispatcher.putItem(localFootprint2);
      }
      Iterator localIterator3 = this.mGroupDispatcher.mGroupList.iterator();
      while (localIterator3.hasNext())
      {
        GroupMapItem localGroupMapItem2 = (GroupMapItem)localIterator3.next();
        if (localGroupMapItem2.isGroup())
        {
          TBarGroupDrawable localTBarGroupDrawable = new TBarGroupDrawable();
          FootprintUtils.boundCenterBottom(localTBarGroupDrawable);
          localGroupMapItem2.setMarker(localTBarGroupDrawable);
          continue;
        }
        Object localObject = null;
        try
        {
          Footprint localFootprint1 = (Footprint)localGroupMapItem2.getFootprints().get(0);
          if ((localFootprint1.mPicPath != null) && (new File(localFootprint1.mPicPath).exists()))
          {
            localObject = (Drawable)this.cachedMap.get(localFootprint1.mPicPath);
            if (localObject == null)
            {
              BitmapFactory.Options localOptions = new BitmapFactory.Options();
              localOptions.inSampleSize = 12;
              Bitmap localBitmap1 = BitmapFactory.decodeFile(localFootprint1.mPicPath, localOptions);
              if (localBitmap1 != null)
              {
                Bitmap localBitmap2 = Bitmap.createScaledBitmap(localBitmap1, 32, 32, false);
                localBitmap1.recycle();
                BitmapDrawable localBitmapDrawable = new BitmapDrawable(localBitmap2);
                TBarDrawable localTBarDrawable = new TBarDrawable(localBitmapDrawable, true);
                localObject = localTBarDrawable;
              }
              this.cachedMap.put(localFootprint1.mPicPath, localObject);
            }
          }
          if (localObject == null)
            localObject = FootprintUtils.getInverseCategory(this.mContext, localFootprint1.mCategory);
          localGroupMapItem2.setMarker((Drawable)localObject);
        }
        catch (OutOfMemoryError localOutOfMemoryError)
        {
          Log.w("AbstractGroupableOverlay", localOutOfMemoryError.getMessage(), localOutOfMemoryError);
          System.gc();
        }
      }
    }
    Iterator localIterator1 = this.mGroupDispatcher.getGroupMapItems().iterator();
    while (localIterator1.hasNext())
    {
      GroupMapItem localGroupMapItem1 = (GroupMapItem)localIterator1.next();
      this.mProjection.toPixels(localGroupMapItem1.mPoint, this.mTempPoint);
      drawAt(paramCanvas, localGroupMapItem1.getMarker(), this.mTempPoint.x, this.mTempPoint.y, paramBoolean);
    }
  }

  public final Footprint getItem(int paramInt)
  {
    return (Footprint)this.mItemList.get(paramInt);
  }

  protected boolean hitTest(GroupMapItem paramGroupMapItem, int paramInt1, int paramInt2)
  {
    return getTouchableBounds(paramGroupMapItem.getMarker().getBounds()).contains(paramInt1, paramInt2);
  }

  public void jump(Footprint paramFootprint, MapView paramMapView)
  {
    while (true)
    {
      Iterator localIterator1 = this.mGroupDispatcher.mGroupList.iterator();
      while (true)
        if (localIterator1.hasNext())
        {
          GroupMapItem localGroupMapItem = (GroupMapItem)localIterator1.next();
          Iterator localIterator2 = localGroupMapItem.getFootprints().iterator();
          if (!localIterator2.hasNext())
            continue;
          if (!paramFootprint.equals((Footprint)localIterator2.next()))
            break;
          if ((localGroupMapItem.getMarker() instanceof TBarDrawable))
          {
            ((TBarDrawable)localGroupMapItem.getMarker()).setFocus(true, paramMapView);
            paramMapView.getController().setCenter(localGroupMapItem.mPoint);
            paramMapView.getController().setZoom(17);
          }
        }
    }
  }

  public boolean onTap(GeoPoint paramGeoPoint, MapView paramMapView)
  {
    paramMapView.getProjection().toPixels(paramGeoPoint, this.mTempPoint);
    ArrayList localArrayList = getItemAtLocation(this.mTempPoint.x, this.mTempPoint.y, paramMapView);
    if (!localArrayList.isEmpty());
    for (boolean bool = onTap((Footprint[])localArrayList.toArray(new Footprint[localArrayList.size()])); ; bool = false)
      return bool;
  }

  protected abstract boolean onTap(Footprint[] paramArrayOfFootprint);

  public boolean onTouchEvent(MotionEvent paramMotionEvent, MapView paramMapView)
  {
    if (paramMotionEvent.getAction() == 0)
    {
      Iterator localIterator2 = checkItemAtLocation((int)paramMotionEvent.getX(), (int)paramMotionEvent.getY(), paramMapView).iterator();
      while (localIterator2.hasNext())
        ((GroupMapItem)localIterator2.next()).getMarker().setState(PRESSED_STATE_SET);
    }
    if ((paramMotionEvent.getAction() == 1) || (paramMotionEvent.getAction() == 3))
    {
      getItemAtLocation((int)paramMotionEvent.getX(), (int)paramMotionEvent.getY(), paramMapView);
      Iterator localIterator1 = this.mGroupDispatcher.mGroupList.iterator();
      while (localIterator1.hasNext())
        ((GroupMapItem)localIterator1.next()).getMarker().setState(new int[0]);
    }
    return false;
  }

  protected final void populate()
  {
    this.mItemList.clear();
    int i = size();
    for (int j = 0; j < i; j++)
    {
      Footprint localFootprint = createItem(j);
      this.mItemList.add(localFootprint);
    }
    changeZoom();
  }

  public void release()
  {
    this.pHandler.getLooper().quit();
    this.cachedMap.clear();
    this.mItemList.clear();
    this.mGroupDispatcher.clear();
    this.mGroupDispatcher.mGroupPool.clear();
    this.mContext = null;
  }

  protected abstract int size();

  private class GroupDispatcher
  {
    private static final double GROUP_RADIUS = 20.0D;
    ArrayList<AbstractGroupableOverlay.GroupMapItem> mGroupList = new ArrayList();
    LinkedList<AbstractGroupableOverlay.GroupMapItem> mGroupPool = new LinkedList();

    private GroupDispatcher()
    {
    }

    public void clear()
    {
      Iterator localIterator = this.mGroupList.iterator();
      while (localIterator.hasNext())
      {
        AbstractGroupableOverlay.GroupMapItem localGroupMapItem = (AbstractGroupableOverlay.GroupMapItem)localIterator.next();
        localGroupMapItem.clear();
        this.mGroupPool.add(localGroupMapItem);
      }
      this.mGroupList.clear();
    }

    public AbstractGroupableOverlay.GroupMapItem getGroupItem(int paramInt)
    {
      return (AbstractGroupableOverlay.GroupMapItem)this.mGroupList.get(paramInt);
    }

    public ArrayList<AbstractGroupableOverlay.GroupMapItem> getGroupMapItems()
    {
      return this.mGroupList;
    }

    public AbstractGroupableOverlay.GroupMapItem newGroupMapItem()
    {
      if (this.mGroupPool.isEmpty());
      for (AbstractGroupableOverlay.GroupMapItem localGroupMapItem = new AbstractGroupableOverlay.GroupMapItem(AbstractGroupableOverlay.this); ; localGroupMapItem = (AbstractGroupableOverlay.GroupMapItem)this.mGroupPool.poll())
        return localGroupMapItem;
    }

    public void putItem(Footprint paramFootprint)
    {
      AbstractGroupableOverlay.this.mProjection.toPixels(paramFootprint.getPoint(), AbstractGroupableOverlay.this.mTempPoint);
      int i = 0;
      Iterator localIterator = this.mGroupList.iterator();
      while (localIterator.hasNext())
      {
        AbstractGroupableOverlay.GroupMapItem localGroupMapItem2 = (AbstractGroupableOverlay.GroupMapItem)localIterator.next();
        AbstractGroupableOverlay.this.mProjection.toPixels(localGroupMapItem2.getPoint(), AbstractGroupableOverlay.this.mTempPoint1);
        if (Math.hypot(AbstractGroupableOverlay.this.mTempPoint.x - AbstractGroupableOverlay.this.mTempPoint1.x, AbstractGroupableOverlay.this.mTempPoint.y - AbstractGroupableOverlay.this.mTempPoint1.y) >= 20.0D)
          continue;
        localGroupMapItem2.addMapItem(paramFootprint);
        i = 1;
      }
      if (i == 0)
      {
        AbstractGroupableOverlay.GroupMapItem localGroupMapItem1 = AbstractGroupableOverlay.this.mGroupDispatcher.newGroupMapItem();
        localGroupMapItem1.addMapItem(paramFootprint);
        this.mGroupList.add(localGroupMapItem1);
      }
    }
  }

  protected class GroupMapItem
  {
    ArrayList<Footprint> mItemList = new ArrayList(1);
    Drawable mMarker;
    GeoPoint mPoint;

    GroupMapItem()
    {
    }

    public void addMapItem(Footprint paramFootprint)
    {
      this.mItemList.add(paramFootprint);
      int i = 0;
      int j = 0;
      Iterator localIterator = this.mItemList.iterator();
      while (localIterator.hasNext())
      {
        Footprint localFootprint = (Footprint)localIterator.next();
        i += localFootprint.getPoint().getLatitudeE6();
        j += localFootprint.getPoint().getLongitudeE6();
      }
      int k = this.mItemList.size();
      this.mPoint = new GeoPoint(i / k, j / k);
    }

    public void clear()
    {
      this.mPoint = null;
      this.mItemList.clear();
      this.mMarker = null;
    }

    public ArrayList<Footprint> getFootprints()
    {
      ArrayList localArrayList = new ArrayList(this.mItemList.size());
      for (int i = 0; i < this.mItemList.size(); i++)
        localArrayList.add(this.mItemList.get(i));
      return localArrayList;
    }

    public Drawable getMarker()
    {
      return this.mMarker;
    }

    public GeoPoint getPoint()
    {
      return this.mPoint;
    }

    public boolean isGroup()
    {
      if (this.mItemList.size() > 1);
      for (int i = 1; ; i = 0)
        return i;
    }

    public void setMarker(Drawable paramDrawable)
    {
      this.mMarker = paramDrawable;
    }
  }

  public static abstract interface OnFocusChangeListener
  {
    public abstract void onFocusChanged(Footprint paramFootprint);
  }
}

/* Location:           E:\workspace\dex2java\dex-translator-0.0.9.3\HtcFootprints_dex2jar.jar
 * Qualified Name:     com.htc.android.footprints.overlay.AbstractGroupableOverlay
 * JD-Core Version:    0.6.0
 */